package com.hiof.moraa.server.service;

import com.hiof.moraa.server.dao.AthleteHealthFeedDAO;
import com.hiof.moraa.server.service.model.Feed;
import com.hiof.moraa.server.service.model.HealthFeed;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Bergy
 * Date: 7/6/12
 * Time: 8:32 AM
 * To change this template use File | Settings | File Templates.
 */
@Transactional(readOnly = false)
public class AthleteHealthFeedServiceImpl implements AthleteHealthFeedService {

    private AthleteHealthFeedDAO dao;

    public void setDao(AthleteHealthFeedDAO dao) {
        this.dao = dao;
    }

    @Override
    public Feed insertHealthFeed(int athleteId, double bloodPressure, double heartRate, String status) {
        HealthFeed feed = dao.insertAthleteLiveLocationFeed(athleteId, new Timestamp(new java.util.Date().getTime()), bloodPressure, heartRate, status);
        return feed;
    }

    @Override
    public List<HealthFeed> getHeartRateHistory(int athleteId) {
        List<HealthFeed> healthHistory = dao.getHealthFeedHistoryOfAthlete(athleteId);
        int size = 100;
        List<HealthFeed> result;
        if (healthHistory.size() > size) {
            result = healthHistory.subList
                    (healthHistory.size() - 1 - size, healthHistory.size() - 1);
        } else {
            result = healthHistory;
        }

        int index = 0;
        for (HealthFeed feed : result) {

            feed.setIndex(index);
            index++;
        }
        return result;
    }
}
